package com.spark.lola.controller.sup;

import com.spark.lola.constant.CertificationStatus;
import com.spark.lola.service.sup.IAccountInfoService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.entity.sup.AccountCertificationApply;
import com.spark.lola.mapper.sup.AccountCertificationApplyMapper;
import com.spark.lola.service.sup.IAccountCertificationApplyService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 认证审核 前端控制器
 * </p>
 * @author dengyejun
 * @since 2020-05-06
 */
@RestController
@RequestMapping("/api/sup/account-certification-apply")
public class AccountCertificationApplyController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(AccountCertificationApplyController.class);
    @Autowired
    private AccountCertificationApplyMapper currentMapper;
    @Autowired
    private IAccountCertificationApplyService currentService;
    @Autowired
    private IAccountInfoService accountInfoService;

    @RequiresPermissions(value = "m5260")
    @RequestMapping(value = "/findBy", consumes = "application/json")
    public ResponseBean findBy(@RequestBody QueryParam<AccountCertificationApply> queryBean,
                               @SessionAttribute ShiroUser shiroUser) {
        PageParam<AccountCertificationApply> pageParam = queryBean.toPageParam();
        IPage<AccountCertificationApply> page = currentMapper.page(pageParam.getPage(), pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @RequiresPermissions(value = "m5264")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.GET)
    public ResponseBean findInfo(@PathVariable Long id,
                                 @SessionAttribute ShiroUser shiroUser) {
        AccountCertificationApply model = currentService.getById(id);

        return ResponseBean.success(model);
    }

    @RequiresPermissions("m5263")
    @RequestMapping(value = "/save", produces = "application/json;charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean save(@RequestBody AccountCertificationApply form, @SessionAttribute ShiroUser shiroUser) throws Exception {
        AccountCertificationApply model = null;
        if(form.getStatus().equals(CertificationStatus.FAILED)){
            model = accountInfoService.authFail(form.getId(),form.getRejectReason());
        }
        if(form.getStatus().equals(CertificationStatus.SUCCESS)){
            model = accountInfoService.authSuccess(form.getId());
        }
        return ResponseBean.success(model);
    }
}
